package www.openteam.com.model;

import android.support.annotation.NonNull;


import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

import www.openteam.com.base.BaseHttpResult;
import www.openteam.com.base.BaseModel;
import www.openteam.com.bean.Result;
import www.openteam.com.bean.ResultNew;
import www.openteam.com.tool.ToastUtil;

/**
 * Created by kylin on 2016/12/19.
 */

public class LoginModel extends BaseModel {
    private static final String TAG = LoginModel.class.getSimpleName();
    private boolean isLogin = false;

    public boolean login(@NonNull String name, @NonNull String pass, @NonNull final LoginOnListener
            loginOnListener) {
        if (loginOnListener == null) {
            throw new RuntimeException("InfoHint不能为空");
        }
        httpApiService.login(name, pass).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<ResultNew>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        isLogin = false;
                        loginOnListener.failInfo(e.getMessage());
                    }

                    @Override
                    public void onNext(ResultNew resultNew) {
                        isLogin = true;
                        String str = "";
                        if (null != resultNew.getData()) {
                            str = resultNew.getInfo();
                            loginOnListener.successInfo(resultNew);
                        }
                    }
                });

        return isLogin;
    }


    public interface LoginOnListener {
        void successInfo(ResultNew result);

        void failInfo(String str);
    }


//    public void onNext(ResultNew result) {
//        isLogin =true;
//        String str="" ;
//        if (result.getData().equals("true")){
//            str ="登陆成功";
//        }
//        loginOnListener.successInfo(result);
//    }
//
//    @Override
//    protected void onError(ApiException e) {
//        super.onError(e);
//        isLogin=false;
//        loginOnListener.failInfo(e.message);
//    }
//});
}
